package com.gwd.task.services.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.format.FastDateFormat;
import com.gwd.task.entity.ProjectEntity;
import com.gwd.task.entity.ProjectNumEntity;
import com.gwd.task.repository.ProjectNumRepository;
import com.gwd.task.services.ProjectNumServer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhiyongming
 * @date 2020/12/3 10:55
 */
@Service
public class ProjectNumServerImpl implements ProjectNumServer {

    @Resource
    private ProjectNumRepository repository;
    private static final String YEAR = "21";

    @Override
    public String getEvent(Long projectId) {
        ProjectNumEntity entity = repository.findByProjectIdAndYear(projectId, YEAR);
        if (entity==null) {
            return "";
        }
        String code = entity.plusP();
        repository.save(entity);
        return code;
    }

    @Override
    public String getTest(Long projectId) {
        ProjectNumEntity entity = repository.findByProjectIdAndYear(projectId, YEAR);
        if (entity==null) {
            return "";
        }
        String code = entity.plusT();
        repository.save(entity);
        return code;
    }

    @Override
    public String getTask(Long projectId) {
        ProjectNumEntity entity = repository.findByProjectIdAndYear(projectId, YEAR);
        if (entity==null) {
            return "";
        }
        String code = entity.plusD();
        repository.save(entity);
        return code;
    }

    @Override
    public String getProductionEventsTask(Long projectId, String code) {
        ProjectNumEntity entity = findByProjectIdAndYear(projectId, code);
        String p = entity.plusP();
        updateEntity(entity);
        return p;
    }

    @Override
    public String getTaskCode(Long projectId, String code) {
        ProjectNumEntity entity = findByProjectIdAndYear(projectId, code);
        String d = entity.plusD();
        updateEntity(entity);
        return d;
    }

    @Override
    public String getTestCode(Long projectId, String code) {
        ProjectNumEntity entity = findByProjectIdAndYear(projectId, code);
        String t = entity.plusT();
        updateEntity(entity);
        return t;
    }

    @Override
    public void saveByProject(ProjectEntity projectEntity) {
        findByProjectIdAndYear(projectEntity.getProjectId(), projectEntity.getCode());

    }

    @Override
    public ProjectNumEntity findByProject(Long projectId) {
        return repository.findByProjectId(projectId);
    }

    @Override
    public List<ProjectNumEntity> findByProjectIdList(List<Long> ids) {
        return repository.findByProjectIdIn(ids);
    }

    private ProjectNumEntity findByProjectIdAndYear(Long projectId, String code) {
        String year = FastDateFormat.getInstance("yy").format(new DateTime());
        ProjectNumEntity entity = repository.findByProjectIdAndYear(projectId, year);
        if (entity==null) {
            ProjectNumEntity newEntity = new ProjectNumEntity();
            newEntity.setCode(code);
            newEntity.setYear(year);
            newEntity.setProjectId(projectId);
            newEntity.setP(0);
            newEntity.setT(0);
            newEntity.setD(0);
            entity = repository.save(newEntity);
        } else {
            if (!entity.getCode().equals(code) && !code.equals("")) {
                entity.setCode(code);
                entity = repository.save(entity);
            }
        }
        return entity;
    }

    private void updateEntity(ProjectNumEntity entity) {
        repository.save(entity);
    }
}
